package com.lc.pianostudent.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.adapter.PianoRecordAdapter;
import com.lc.pianostudent.bean.PianoRecordBean;
import com.lc.pianostudent.conn.PostGetRecord;
import com.lc.pianostudent.conn.PostJournalIndex;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/25.
 */

public class PianoRecordActivity extends BaseActivity{

    public List<PianoRecordBean> list = new ArrayList<>();
    @BoundView(R.id.piano_record_recycleview) private XRecyclerView recyclerView;
    private PianoRecordAdapter pianoRecordAdapter;
    private PostJournalIndex.PostJournalIndexInfo currentInfo;
    @BoundView(R.id.zhanwei) private ImageView zhanwei;
    @BoundView(R.id.piano_text) private TextView piano_text;

    private PostJournalIndex postJournalIndex = new PostJournalIndex(new AsyCallBack<PostJournalIndex.PostJournalIndexInfo>() {

        @Override
        public void onSuccess(String toast, int type, Object o, PostJournalIndex.PostJournalIndexInfo postJournalIndexInfo) throws Exception {

            if(type == 0){
                list.clear();
            }

            currentInfo = postJournalIndexInfo;

            list.addAll(postJournalIndexInfo.list);
            if(list.size() == 0){
                zhanwei.setVisibility(View.VISIBLE);
                piano_text.setVisibility(View.VISIBLE);
            }else {
                zhanwei.setVisibility(View.INVISIBLE);
                piano_text.setVisibility(View.INVISIBLE);
                pianoRecordAdapter.notifyDataSetChanged();

            }
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {
            recyclerView.refreshComplete();
            recyclerView.loadMoreComplete();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }

    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_piano_record);

        setBackTrue();
        initView();
    }

    private void initView(){
        setTitleName("练琴日志");


        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        pianoRecordAdapter = new PianoRecordAdapter(this, list);
        recyclerView.setAdapter(pianoRecordAdapter);
        postJournalIndex.user_id = BaseApplication.BasePreferences.readUID();

        postJournalIndex.execute();

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                postJournalIndex.user_id = BaseApplication.BasePreferences.readUID();

                postJournalIndex.execute(context,false,0);
            }

            @Override
            public void onLoadMore() {
                recyclerView.refreshComplete();
                recyclerView.loadMoreComplete();
            }
        });


    }


}
